package com.benjaminwan.read.free.ui.keyboard

import android.app.Service
import android.content.Context
import android.content.res.Configuration
import android.os.Bundle
import android.os.VibrationEffect
import android.os.Vibrator
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import com.airbnb.mvrx.activityViewModel
import com.airbnb.mvrx.withState
import com.benjaminwan.read.free.R
import com.benjaminwan.read.free.base.fragment.BaseFragment
import com.benjaminwan.read.free.base.viewmodel.state
import com.benjaminwan.read.free.databinding.FragmentKeyboardBinding
import com.benjaminwan.read.free.model.Keyboard
import com.benjaminwan.read.free.ui.main.MainViewModel
import com.benjaminwan.read.free.ui.settings.SettingsViewModel
import com.benjaminwan.read.free.utils.RotationMode

class KeyboardFragment : BaseFragment(), Keyboard.KeyboardListener {

    private val mainVM by activityViewModel(MainViewModel::class)
    private val settingsVM by activityViewModel(SettingsViewModel::class)

    private val ledON by lazy {
        ContextCompat.getColor(requireContext(), R.color.material_green_600)
    }

    private val ledOFF by lazy {
        ContextCompat.getColor(requireContext(), R.color.material_grey_600)
    }

    private val vibrator: Vibrator by lazy {
        requireContext().getSystemService(Service.VIBRATOR_SERVICE) as Vibrator
    }

    private val vibratorEnable: Boolean by lazy {
        settingsVM.state.vibratorEnable
    }

    private val vibrationEffect = VibrationEffect.createOneShot(
        100, VibrationEffect.DEFAULT_AMPLITUDE
    )

    override var rotationMode: RotationMode = RotationMode.ALL

    private lateinit var binding: FragmentKeyboardBinding

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentKeyboardBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initViews()
        if (vibratorEnable)
            vibrator.vibrate(vibrationEffect)
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        updateViews()
    }

    private fun updateViews() {
        val rootViewGroup = binding.root
        if (rootViewGroup is ViewGroup) {
            rootViewGroup.removeAllViews()
            View.inflate(requireContext(), R.layout.fragment_keyboard, rootViewGroup)
            initViews()
        }
    }

    private fun createKeyboard(
        context: Context, xmlResourceID: Int
    ): Keyboard {
        val keyboard = Keyboard(context, xmlResourceID)
        keyboard.setKeyboardListener(this)
        return keyboard
    }

    private fun initViews() {
        val qwertyKeyboard = createKeyboard(requireContext(), R.xml.qwerty_keyboard)
        val navigationKeyBoard = createKeyboard(requireContext(), R.xml.navigation_keyboard)
        val numericKeyBoard = createKeyboard(requireContext(), R.xml.numeric_keyboard)
        binding.qwertyKeyLayout.addView(qwertyKeyboard)
        binding.navigationKeyLayout.addView(navigationKeyBoard)
        binding.numericKeyLayout.addView(numericKeyBoard)
    }

    override fun onKeyDown(keyCode: Int) {
        mainVM.keyboardKeyDown(keyCode)
        if (vibratorEnable)
            vibrator.vibrate(vibrationEffect)
    }

    override fun onKeyUp(keyCode: Int) {
        mainVM.keyboardKeyUp(keyCode)
    }

    override fun invalidate() = withState(mainVM) { main ->
        requireActivity().invalidateOptionsMenu()
        binding.numLockTv.setBackgroundColor(if (main.numLock) ledON else ledOFF)
        binding.capsLockTv.setBackgroundColor(if (main.capsLock) ledON else ledOFF)
        binding.scrollLockTv.setBackgroundColor(if (main.scrollLock) ledON else ledOFF)
    }


}